package cz.jm.util.logging;

import java.net.URL;
import java.util.logging.LogManager;


/**
 * Realizacia konfiguracie loggeru z java.util.logging pomocou konfiguracneho suboru. 
 * */
public class LoggerClassPathConfigurator {
	
	public static final String DEFAULT_LOGGING_PROPERTIES_FILE = "logging.properties"; 
	
	
	/** Konfiguracia loggeru z java.util.logging pomocou suboru 
	 * {@link LoggerClassPathConfigurator#DEFAULT_LOGGING_PROPERTIES_FILE}, ktory sa nachadza v classpath. */
	public void configure() {
		configure(DEFAULT_LOGGING_PROPERTIES_FILE);
	}

	/** Konfiguracia loggeru z java.util.logging pomocou suboru fileName, ktory sa nachadza v classpath. */
	public void configure(String fileName) {
		  String loggingfile = System.getProperty("java.util.logging.config.file");
		  if (loggingfile == null || loggingfile.isEmpty()) {
		    URL url = null;
		    try {
		      url = LoggerClassPathConfigurator.class.getClassLoader().getResource(fileName);
		      if (url == null) {
		        System.err.println("Cannot find logging.properties.");
		      } else {
		      LogManager.getLogManager().readConfiguration(url.openStream());
		      }
		    } catch (Exception e) {
		    	System.err.println("Error reading logging.properties from '" + url + "': " + e);
		    }
		  }
		}
}
